﻿package core.display {
	
	import flash.display.SimpleButton;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import core.events.BookCaseEvent;
	import core.events.BlueToothConnectEvent;
	import core.User;
	import core.dialog.DialogBluetoothDiscovery;
	import core.dialog.DialogBluetooth;
	import base.BaseMc;
	public class BtnClass extends SimpleButton {
		
		
		public function BtnClass() {
			this.addEventListener(Event.ADDED_TO_STAGE,onInit);
			this.addEventListener(Event.REMOVED_FROM_STAGE,onRemove);
			
		}
		//连接成功
		private function bluetoothConnect(e:BlueToothConnectEvent):void
		{
			  trace("连接成功连接成功连接成功连接成功");
			   var mc:BaseMc=e.target as BaseMc;
			   if(mc!=null){
					mc.remove();
			   }
			  this.dispatchEvent(new BookCaseEvent(BookCaseEvent.CLASS_CLICKED));
		}
		//连接失败
		private function bluetoothFailed(e:BlueToothConnectEvent):void
		{
			  trace("连接失败连接失败连接失败连接失败");
		}
		private function onInit(e:Event):void {
			this.removeEventListener(Event.ADDED_TO_STAGE,onInit);
			this.addEventListener(MouseEvent.CLICK,onClick);
			this.onClick(null);
		}
		
		private function onRemove(e:Event):void {
			this.removeEventListener(MouseEvent.CLICK,onClick);
		}
		
		private function onClick(e:MouseEvent):void {
			if(User.deviceAddress == null || User.deviceAddress == "" || User.deviceAddress == "0")
			{
				var dlg1:DialogBluetoothDiscovery = new DialogBluetoothDiscovery();
				dlg1.addEventListener(BlueToothConnectEvent.BLUETOOTHCONNECT,bluetoothConnect); 
					this.stage.addChild(dlg1);
			}
			else
			{
				var dlg2:DialogBluetooth = new DialogBluetooth(User.deviceAddress);
				dlg2.addEventListener(BlueToothConnectEvent.BLUETOOTHCONNECT,bluetoothConnect); 
					this.stage.addChild(dlg2);
			}
			//
			//this.dispatchEvent(new BookCaseEvent(BookCaseEvent.CLASS_CLICKED));
		}
		
	}
	
}
